package com.chai.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.chai.constant.MessageConstant;
import com.chai.constant.RedisMessageConstant;
import com.chai.service.OrderService;
import com.chai.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/Order")
public class OrderController {

    @Reference
    OrderService orderService;

    @Autowired
    JedisPool jedisPool;

    @PostMapping("/Submit")

    public Result Submit(@RequestBody Map map) {
        try {
            String validateCode = (String) map.get("validateCode");
            String telephone = (String) map.get("telephone");

            String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
            if ((redisCode == null || !redisCode.equals(validateCode))) {
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }
            Result result = orderService.saveOrder(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }
    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
